package com.jerry.mingxing.ui.categroycategroy;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;

import com.jerry.mingxing.R;
import com.jerry.mingxing.bean.CategroyBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/30.
 */
public class CategroyCategroyFragment extends Fragment implements CategroyCategroyContract.View {

    @Bind(R.id.categroyfragment_left_lv)
    ListView categroyfragmentLeftLv;
    @Bind(R.id.categroyfragment_left_gv)
    GridView categroyfragmentLeftGv;
    private View view;
    private Activity activity;
    private List<CategroyBean.DataBean.ItemsBean> totallist= new ArrayList<>();
    private List<CategroyBean.DataBean.ItemsBean.ComponentBean.ItemsBean_inner> totalistContent=new ArrayList<>();
    private CategroyTitleAdapter cateGroy_leftAdapter;
    private CategroyShowAdapter cateGroy_rightAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.categroyfragment_left, container, false);
        ButterKnife.bind(this, view);
        CategroyCategroyPresenter presenter =new CategroyCategroyPresenter(this);
        presenter.getData();

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResponse(CategroyBean cbean) {
        setAdapter(cbean);
        setListener(cbean);
    }

    private void setListener(final CategroyBean cbean) {
        categroyfragmentLeftGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //品类的gridview点击事件
            }
        });
        categroyfragmentLeftLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                cateGroy_rightAdapter.reloadData(cbean.getData().getItems().get(position).getComponent().getItemsinner());
            }
        });
    }

    private void setAdapter(CategroyBean cbean) {
        totallist=cbean.getData().getItems();
        cateGroy_leftAdapter=new CategroyTitleAdapter(totallist,activity) ;
        categroyfragmentLeftLv.setAdapter(cateGroy_leftAdapter);
        totalistContent=cbean.getData().getItems().get(0).getComponent().getItemsinner();
        cateGroy_rightAdapter=new CategroyShowAdapter(totalistContent,activity);
        categroyfragmentLeftGv.setAdapter(cateGroy_rightAdapter);
    }

    @Override
    public void onFailure(String s) {

    }


}
